﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Threading;
using System.Threading.Tasks;
using Lumberjack.BusinessLogic;

namespace Lumberjack
{
    /// <summary>
    /// Interaction logic for AddCompetitor.xaml
    /// </summary>
    public partial class AddCompetitor : Window
    {
        public delegate void Refresh();  //delegate - deklaracja funkcji ktora zwarca cos tam i przyjmuje cos tam
        private Refresh _refresh;              //obiekt funkcji refresh
        IRepository<Contest> _contestService;
        IRepository<Competitor> _competitorService;

        public AddCompetitor(Refresh refresh)
        {
            InitializeComponent();
            _competitorService = new RepositoryService<Competitor>(MainWindow.context);
            _contestService = new RepositoryService<Contest>(MainWindow.context);
            _refresh = refresh;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            if (tbName.Text == "" || tbSurname.Text == "" || tbNumber.Text == "")
            {
                return;
            }
          
           

                Competitor competitor = new Competitor()
                {


                    C_name = tbName.Text,
                    surname = tbSurname.Text,
                    id_contest = CompetitionWindow.contest.Id,
                    Contest = _contestService.GetById(CompetitionWindow.contest.Id),
                    czas = "0",
                    number = Convert.ToInt32(tbNumber.Text)
                };

                if (CompetitionWindow.contest.competition_1 > 0)
                {
                    competitor.punkty = "0";
                }
                else if (CompetitionWindow.contest.competition_2 > 0)
                {
                    competitor.punkty = "0";
                }
                else if (CompetitionWindow.contest.competition_3 > 0)
                {
                    competitor.punkty = "0";
                }
                else if (CompetitionWindow.contest.competition_4 > 0)
                {
                    competitor.punkty = "0";
                }
                else
                {
                    competitor.punkty = "0";
                }
                if (nadlesnictwo.IsChecked == true)
                {
                    competitor.adm_unit = 0;
                }
                else if (dyrekcja.IsChecked == true)
                {
                    competitor.adm_unit = 2;
                }
                else
                {
                    competitor.adm_unit = 1;
                }

                _competitorService.Insert(competitor);
                CompetitionWindow.competitors.Add(competitor);  // a to jest observable collections czyli lista teoretycznie w ListView

                _refresh();

                tbName.Text = null;
                tbSurname.Text = null;
                tbNumber.Text = null;
            }

        

        private void tbNumber_KeyDown(object sender, KeyEventArgs e)
        {
            Validation.OnlyNumbersAndCommas(e, tbNumber);
        }

    }
}
